//
//  RootBarViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "RootBarViewController.h"
#import "CinemaViewController.h"
#import "MoreViewController.h"
#import "MovieViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"

#import "BaseViewController.h"
#import "BaseNavigationController.h"
#import "JWTabBarButton.h"
#import "constant.h"



CGFloat barH = 49;

CGFloat btnW = 50;
CGFloat btnH = 40;

CGFloat selectedImgW = 55;
CGFloat selectedImgH = 49;

@interface RootBarViewController ()
@property (nonatomic, strong) NSArray *vcArray;
@property (nonatomic, strong) UIImageView *selectedImage;
@property (nonatomic, assign) CGFloat pading;

@end

@implementation RootBarViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addControllers];
    [self reloadTabbar];
    [self addSelectedImage];
    self.tabBar.translucent = YES;

}

- (void)addControllers{
    
    MovieViewController *movieVC = [[MovieViewController alloc] init];
    NewsViewController *newsVC = [[NewsViewController alloc] init];
    TopViewController *TopVC = [[TopViewController alloc] init];
    CinemaViewController *cinemaVC = [[CinemaViewController alloc] init];
    MoreViewController *moreVC = [[MoreViewController alloc] init];
    
    _vcArray = @[movieVC,newsVC,TopVC,cinemaVC,moreVC];
    NSMutableArray *barSubVCArray = [NSMutableArray array];
    
    for (int i = 0; i < _vcArray.count; i++) {
        BaseNavigationController *nvg = [[BaseNavigationController alloc] initWithRootViewController:_vcArray[i]];
        [nvg.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                         [UIColor whiteColor],
                                                   NSForegroundColorAttributeName,nil]];
        
        [barSubVCArray addObject:nvg];
    }
    
    self.viewControllers = barSubVCArray;
    
}

- (void)reloadTabbar{
    
    for (UIView *obj in self.tabBar.subviews) {
        [obj removeFromSuperview];
    }
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    _pading = (JWScreenWidth - btnW * _vcArray.count) / (_vcArray.count + 1);
    
    NSArray *btnImageAr = @[@"movie_home",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"];
    NSArray *btnTitleAt = @[@"电影",@"新闻",@"Top",@"影院",@"更多"];
    
    for (int i = 0; i < _vcArray.count; i++) {
        
        JWTabBarButton *mBarButton = [JWTabBarButton tabBarButtonWithIcon:btnImageAr[i] title:btnTitleAt[i]];
        mBarButton.frame = CGRectMake((btnW + _pading) * i + _pading, (barH - btnH) * 0.5, btnW, btnH);
        mBarButton.tag = 100 + i;
        
        [mBarButton addTarget:self action:@selector(barBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabBar addSubview:mBarButton];
    }
    
}

- (void)addSelectedImage{
    _selectedImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, (barH - selectedImgH ) * 0.5, selectedImgW, selectedImgH)];
    _selectedImage.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    JWTabBarButton *btn = self.tabBar.subviews.firstObject;
    _selectedImage.center = btn.center;
    [self.tabBar insertSubview:_selectedImage belowSubview:self.tabBar.subviews.firstObject];//放到按钮下面
}

- (void)barBtnAction:(JWTabBarButton *)btn{
    self.selectedIndex = btn.tag - 100;
    
    [UIView beginAnimations:nil context:nil];
    _selectedImage.center = btn.center;
    [UIView animateWithDuration:1 animations:nil];
    [UIView commitAnimations];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
